package com.educate.book.web.controller;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.educate.book.model.bo.BookBO;
import com.educate.book.rpc.api.BookService;
import com.educate.common.web.R;

import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("book")
public class BookController {

    @Reference(version = "${api.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}",timeout=2000)
    private BookService bookService;

    /**
     *  请求参数：{"desc":"0-3岁","name":"0-3岁","parentId","XXXXXXXX"}
     *  desc:描述
     *  name:名称
     *  parentId：父ID
     *  
 		返回结果:{"code":"000000","msg":"success","data":"{\"id\":\"a19a467237b2470e98a5ccb4286c3ca1\"}"}
 		id:为新增成功的ID
 		
 		返回结果：{"code":"000001","msg":"name duplicate"}
 		名称重复
     * @param bo
     * @return
     */
    @PostMapping(value = "/create")
    public Object create(@RequestBody BookBO bo) {
    	String Id = bookService.create(bo);
    	JSONObject json = new JSONObject();
    	if("-1".equals(Id)) {
    		json.put("code", "000001");
    		json.put("msg", "name duplicate");
    		return json.toJSONString();
    	}
    	json.put("id", Id);
        return R.ok(json.toJSONString());
    }

    /**
     *  请求参数：{"author":"test","bookId":"7ab7165e8402499ebe5b97c6def24ba5","bookName":"少儿植物认知","bookNumber":"Sffdf88fd88779","copyrightStatement":"copyrightStatement","edition":2,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"pricing":10.5,"publisherName":"中国出版社","publisherPlace":"广东深圳"}
 		返回结果:{"code":"000000","msg":"success"}
     * @param bo
     * @return
     */
    @PostMapping(value = "/delete")
    public R delete(@RequestBody BookBO bo) {
    	bookService.delete(bo);
        return R.ok();
    }
    /**
     * 根据ID删除分类
     * url:http://xxxxxx:port/delete/{bookId}
     * @param bookId
     * @return
     */
    @PostMapping(value = "/delete/{bookId}")
    public R delete(@PathVariable String bookId) {
    	bookService.delete(bookId);
        return R.ok();
    }

    /**
     * 
     *  请求URL：http://localhost:8080/book/detail/7bb515a5ed7f4bf295bb93ecdefdf71a
		请求参数：{"author":"test","bookId":"7bb515a5ed7f4bf295bb93ecdefdf71a","edition":0}
		返回结果:{"code":"000000","msg":"success","data":{"bookId":"7bb515a5ed7f4bf295bb93ecdefdf71a","bookName":"少儿植物认知--第一篇001","author":"test","publisherPlace":"广东深圳","copyrightStatement":"copyrightStatement","pricing":10.5,"bookNumber":"Sffdf88fd88779","edition":0,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"publisherName":"中国出版社"}}
     * @param bookId
     * @return
     */
    @PostMapping(value = "/detail/{bookId}")
    public R<BookBO> detail(@PathVariable String bookId) {
    	BookBO bo = bookService.detail(bookId);
        return R.ok(bo);
    }

    /**
     *   请求URL：http://localhost:8080/book/detail
		 请求参数：{"author":"test","bookId":"7bb515a5ed7f4bf295bb93ecdefdf71a","edition":0}
		 返回结果:{"code":"000000","msg":"success","data":{"bookId":"7bb515a5ed7f4bf295bb93ecdefdf71a","bookName":"少儿植物认知--第一篇001","author":"test","publisherPlace":"广东深圳","copyrightStatement":"copyrightStatement","pricing":10.5,"bookNumber":"Sffdf88fd88779","edition":0,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"publisherName":"中国出版社"}}

     * @param bo
     * @return
     */
    @PostMapping(value = "/detail")
    public R<BookBO> detail(@RequestBody BookBO bo) {
    	BookBO detail = bookService.detail(bo);
        return R.ok(detail);
    }

    /**
     *  请求参数：{"author":"test","bookId":"7bb515a5ed7f4bf295bb93ecdefdf71a","bookName":"少儿植物认知--第一篇","bookNumber":"Sffdf88fd88779","copyrightStatement":"copyrightStatement","edition":2,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"pricing":10.5,"publisherName":"中国出版社","publisherPlace":"广东深圳"}
 		返回结果:{"code":"000000","msg":"success"}
     * @param bo
     * @return
     */
    @PostMapping(value = "/update")
    public R<BookBO> update(@RequestBody BookBO bo) {
    	bookService.update(bo);
        return R.ok();
    }

    /**
     * 查询条件可为：ISBN、bookName、bookNameEn、author、briefIntroduction、status、featured、recommend、bookNumber
     *   请求URL：http://localhost:8080/book/query
		 请求参数：{"author":"test"}
		 返回结果:{"code":"000000","msg":"success","data":{"content":[{"bookId":"7bb515a5ed7f4bf295bb93ecdefdf71a","bookName":"少儿植物认知--第一篇001","author":"test","publisherPlace":"广东深圳","copyrightStatement":"copyrightStatement","pricing":10.5,"bookNumber":"Sffdf88fd88779","edition":0,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"publisherName":"中国出版社"},{"bookId":"510cdc63caae455cb5c9551b56e61379","bookName":"少儿植物认知","author":"test","publisherPlace":"广东深圳","copyrightStatement":"copyrightStatement","pricing":10.5,"bookNumber":"Sffdf88fd88779","edition":2,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"publisherName":"中国出版社"},{"bookId":"8202d0a749774829b934d38868a8b19c","bookName":"少儿植物认知","author":"test","publisherPlace":"广东深圳","copyrightStatement":"copyrightStatement","pricing":10.5,"bookNumber":"Sffdf88fd88779","edition":2,"item":["6f51f69ef6cc4aae96c467d3b15f10ef","03f37512b69f46968b561f8d2374bcca"],"publisherName":"中国出版社"}],"size":2147483647,"number":0,"totalPages":1,"first":true,"numberOfElements":3,"last":true,"totalElements":3}}

     * @param bo
     * @return
     */
    @PostMapping(value = "/query")
    public R<Page<BookBO>> query(@RequestBody BookBO bo) {
        Page<BookBO> result = bookService.query(bo);
        return R.ok(result);
    }

     

}
